﻿using System;
using System.Collections.Generic;
using System.Linq;
using SapaFinance.Domain.Model;
using Xunit;

namespace SapaFinance.Tests
{
    public class OrderTest
    {
        private User user;
        private BudgetItem budgetitem;
        private PaymentType paymentType;
        private Corporation corporation;
        private Currency currency;
        private Bank bank;

        public OrderTest()
        {
            user = UserTest.Create();
            currency = new Rub();
            bank = BankTest.Create();
            corporation = CorporationTest.Create();
            paymentType = PaymentTypeTest.Create();
            budgetitem = BudgetItemTest.Create();
        }

        private Request InitRequest(decimal payment)
        {

            var acc = new Account("345345", currency, bank, corporation, user);
            var request = new Request(user)
            {
                PaymentPurpose = "Описание назначения платежа",
                Description = "Текст, оставленный автором",
                Unique = 567,
                PaymentPeriod = DateTime.Now
            };
            request.SetPayer(corporation);
            corporation.AddAccount(acc);
            request.SetPayee(corporation);
            request.SetPaymentType(paymentType);
            request.SetPayeeAccount(acc);
            request.SetPayment(payment);
            request.SetTaxRate(18);
            request.SetBudgetItem(budgetitem);
            return request;
        }

        private Order InitOrder(decimal amount)
        {
            var order = new Order(user, amount, currency);
            order.SetAgent(AgentTest.Create());
            order.PaymentPeriod = DateTime.Now;
            order.Execute(user);
            return order;
        }


        [Fact]
        public void AddRequestsToOrder()
        {
            //act
            var requests = new List<Request> { InitRequest(400m) };
            var order = InitOrder(900m);
            foreach (var request in requests)
            {
                // Assert.True(order.MayToBeAdded(request));
                order.AddRequest(request);
            }
            var request3 = InitRequest(500m);
            //Assert.True(order.MayToBeAdded(request3));
            order.AddRequest(request3);
            Assert.Equal(2, order.PartialPayment.Count());
            var request2 = InitRequest(0.01m);
            //Assert.False(order.MayToBeAdded(request2));
            order.AddRequest(request2);
            Assert.Equal(2, order.PartialPayment.Count());
        }

        [Fact]
        public void AddRequestsToOrderPercentComplete()
        {
            var r1 = InitRequest(1000m);
            var r2 = InitRequest(5000m);
            var r3 = InitRequest(2000m);
            var r4 = InitRequest(1500m);
            var order = InitOrder(9200m);
            order.AddRequest(r1);
            Assert.True(r1.PaidPercentByOrder()==100);
            order.AddRequest(r4);
            Assert.True(r4.PaidPercentByOrder() == 100);
            order.AddRequest(r3);
            Assert.True(r3.PaidPercentByOrder() == 100);
            order.AddRequest(r2);
            var notpaidamount = r2.GetNotPaidAmount();
            Assert.Equal(300, notpaidamount.Amount);
            var paidamount = r2.GetPaidAmount(r2.Payment.Currency);
            Assert.Equal(4700, paidamount.Amount);
            Assert.Equal(94, r2.PaidPercentByOrder());
            Assert.Equal(4, order.PartialPayment.Count());
            //var amount = order.GetNotPaidAmount();
            //Assert.Equal(Money.Create(300m, currency), amount);
        }
    }
}